package com.baichang.taoshunda.service.impl;

import com.baichang.framework.excelption.ServiceException;
import com.baichang.framework.utils.LogUtils;
import com.baichang.taoshunda.job.AddCompanyMoneyJob;
import com.baichang.taoshunda.service.TwoSchedulerService;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.GroupMatcher;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Set;

/**
 * Created by Administrator on 2018/7/7 0007.
 */
@Service
public class TwoSchedulerServiceImpl implements TwoSchedulerService {

    private Scheduler sched = null;

    public TwoSchedulerServiceImpl() {
        try {
            SchedulerFactory sf = new StdSchedulerFactory();
            sched = sf.getScheduler();
        } catch (Exception e) {

        }
    }

    @Override
    public void start() {
        try {
            if (sched != null) {
                sched.start();
            }
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void close() {
        try {
            if (sched != null) {
                sched.shutdown(true);
            }

        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean deleteAllJob() throws ServiceException {
        if (sched == null) {
            throw new ServiceException("scheduler is null ");
        }
        GroupMatcher<JobKey> matcher = GroupMatcher.anyJobGroup();
        Set<JobKey> jobKeys = null;
        try {
            jobKeys = sched.getJobKeys(matcher);
        } catch (SchedulerException e) {
            throw new ServiceException(e);
        }
        if (null != jobKeys) {
            for (JobKey jobKey : jobKeys) {
                deleteJob(jobKey);
            }
        }
        return true;
    }

    @Override
    public boolean deleteJob(JobKey jobKey) throws ServiceException {
        if (sched == null) {
            throw new ServiceException("scheduler is null ");
        }
        try {
            sched.deleteJob(jobKey);
        } catch (SchedulerException e) {
            throw new ServiceException(e);
        }
        return true;
    }

    @Override
    public JobKey addSchedulerByCron(String timing, Class<AddCompanyMoneyJob> jobClazz, String agentId) throws ServiceException {
        LogUtils.getInstance().info("=======================");
        if (this.sched == null) {
            throw new ServiceException("scheduler is null ");
        } else {
            JobDetail job = JobBuilder.newJob(jobClazz).build();
            CronTrigger trigger = (CronTrigger) TriggerBuilder.newTrigger().withIdentity(jobClazz.getName(), "repeatday_group").withSchedule(CronScheduleBuilder.cronSchedule(timing)).startAt(new Date()).build();
            JobKey jobKey = null;

            try {
                this.sched.scheduleJob(job, trigger);
                jobKey = job.getKey();
                return jobKey;
            } catch (SchedulerException var8) {
                throw new ServiceException(var8);
            }
        }
    }
}
